<?php


namespace app\api\controller;


use app\data\controller\api\Auth;
use app\data\model\DataQiyeRzinfo;
use app\data\model\DataUser;
use app\data\model\DataUserCollection;
use app\data\model\DataUserReview;
use app\data\model\Loans;
use app\data\model\LoansApplication;
use app\data\model\Parks;
use app\data\model\RecruitmentReview;
use app\data\model\RecruitmentTalent;

/**
 * 需要登录的接口提交都在这
 * Class Submit
 * @package app\api\controller
 */
class Submit extends Auth
{
    /**
     * 贷款申请
     */
    public function loanApply()
    {
        $loanId = input('loan_id');
        if (!$loanId) {
            return_msg(404, '参数错误');
        }
        $loan = Loans::query()->where('id', $loanId)->where('status', 1)->where('deleted', 0)->find();
        if (!$loan) {
            return_msg(404, '当前贷款产品不存在或已删除');
        }
        LoansApplication::query()->insert([
            'user_id'  => $this->uuid,
            'loans_id' => $loanId,
        ]);
        return_msg(200, 'success');
    }


    /**
     * 用户收藏
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function collection()
    {
        $type = input('type');
        if (!in_array($type, [DataUserCollection::COLLECTION_TYPE_RESUME, DataUserCollection::COLLECTION_TYPE_PARK, DataUserCollection::COLLECTION_TYPE_LOANS])) {
            return_msg(404, '参数错误');
        }
        $collectionId = input('collection_id');
        /**
         * @var DataUserCollection $collection
         */
        $collection = DataUserCollection::query()
            ->where('user_id', $this->uuid)
            ->where('collection_id', $collectionId)
            ->where('type', $type)->find();
        if ($collection) {
            $collection->delete();
            return_msg(200, '已取消');
        }

        switch ($type) {
            case DataUserCollection::COLLECTION_TYPE_RESUME:
                /**
                 * @var RecruitmentTalent $collection
                 */
                $collection = RecruitmentTalent::query()->where('id', $collectionId)->find();
                break;
            case DataUserCollection::COLLECTION_TYPE_PARK:
                /**
                 * @var Parks $collection
                 */
                $collection = Parks::query()->where('id', $collectionId)->find();
                break;
            case DataUserCollection::COLLECTION_TYPE_LOANS:
                /**
                 * @var Loans $collection
                 */
                $collection = Loans::query()->where('id', $collectionId)->find();
                break;
        }
        if (!$collection) {
            return_msg(404, '收藏对象不存在');
        }
        DataUserCollection::query()->insert([
            'user_id'       => $this->uuid,
            'collection_id' => $collectionId,
            'type'          => $type,
        ]);
        return_msg(200, '已收藏');
    }
}
